package com.buba.record.controller;


import com.buba.pojo.record.Record;
import com.buba.record.service.RecordService;
import com.buba.record.vo.RecordVo;
import com.buba.utils.R;
import com.buba.utils.RandomUtil;
import com.buba.utils.RedisUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.buba.pojo.other.DivisionType;
import com.buba.pojo.record.WaitingRecord;
import com.buba.pojo.user.Campus;
import com.buba.pojo.user.Doctor;
import com.buba.pojo.user.ThePatient;
import com.buba.record.service.RecordService;
import com.buba.record.vowl.ConsumptionVoWL;
import com.buba.record.vowl.RecordVoWL;
import com.buba.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.math.BigDecimal;

import java.util.Date;

/**
 * <p>
 * 记录表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@Slf4j
@RestController
@RequestMapping("/record/record")
public class RecordController {
    @Autowired
    private RecordService rService;


    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RecordService recordService;

    //查询逐月充值记录
    @GetMapping("/patientRecordById/{id}/{begin}/{end}")
    public R patientRecordById(@PathVariable Integer id , @PathVariable String begin , @PathVariable String end){
        log.info("id:::"+id);
        log.info("begin:::"+begin);
        log.info("end:::"+end);
        List<RecordVo> recordVos = recordService.patientRecordById(id , begin , end);
        log.info("recordVos:::"+recordVos);
        return R.ok().data("record" , recordVos);
    }

    /**
    * todo 充值记录
    * @param record
    * @return com.buba.utils.R
    * @author xhh
    * @data 2022/6/13 10:51
    */
    @RequestMapping(value = "/payRecord",method = RequestMethod.POST)
    public R payRecord(@RequestBody Record record){
        log.info("============record RecordController payRecord=============param{}",record);
        //QueryWrapper queryWrapper = new QueryWrapper();
        //queryWrapper.eq("user_id",userId);
        boolean save = rService.save(record);
        if(save) {
            return R.ok();
        }
        else {
            return R.error();
        }
    }


    /*
     * @Author: 张庆威
     * @Description: (生成挂号缴费记录)
     * @Date: 20:26 2022/6/16
     * @Param: [record]
     * @return: com.buba.utils.R
     **/
    @RequestMapping(value = "/addjilu",method = RequestMethod.POST)
    public R addjilu(@RequestBody Record record){

        log.info("record:::"+record);
        record.setRechargeExplain("挂号缴费");
        record.setCreationTime(new Date());
        record.setModificationTime(new Date());
        boolean save = rService.save(record);
        if(save) {
            return R.ok();
        }
        else {
            return R.error();
        }
    }


    /*
     * @Author: 张庆威
     * @Description: (生成体检预约记录 记录表)
     * @Date: 19:58 2022/6/21
     * @Param: [record]
     * @return: com.buba.utils.R
     **/
    @RequestMapping(value = "/addTijianJIlu",method = RequestMethod.POST)
    public R addTijianJIlu(@RequestBody Record record){

        log.info("record:::"+record);
        record.setRechargeExplain("体检消费");
        record.setDivisionTypeId(43);
        record.setCreationTime(new Date());
        record.setModificationTime(new Date());
        boolean save = rService.save(record);
        if(save) {
            return R.ok();
        }
        else {
            return R.error();
        }
    }


    //药品记录详情
    @GetMapping("/drugRecordById/{id}")
    public R drugRecordById(@PathVariable Integer id){
        RecordVo record = recordService.drugRecordById(id);
        return R.ok().data("record" , record);
    }

    //账单详情
    @GetMapping("/recordById/{id}")
    public R recordById(@PathVariable Integer id){
        RecordVo record = recordService.recordById(id);
        return R.ok().data("record" , record);
    }

    //根据就诊人id查询卡内余额
    @GetMapping("/cardBalance/{id}")
    public R cardBalance(@PathVariable Integer id){
        log.info("cardBalance:::"+id);
        RecordVo PatientCard = recordService.cardBalance(id);
        return R.ok().data("balance" , PatientCard);
    }

    //根据日期范围查询充值记录
    @GetMapping("/recordDate/{id}/{begin}/{end}")
    public R recordDate(@PathVariable Integer id , @PathVariable String begin , @PathVariable String end) {
        log.info("id::" + id);
        log.info("begin::" + begin);
        log.info("end::" + end);

        List<RecordVo> record = recordService.recordDate(id , begin , end);

        return R.ok().data("record" , record);
    }

    //门诊充值记录
    @GetMapping("/patientSkype/{id}")
    public R patientSkype(@PathVariable Integer id){
        List<RecordVo> record = recordService.patientSkype(id);
        return R.ok().data("record" , record);
    }

    /**
     * @Author 梁玉杰
     * @Description //TODO
     * @param Record record
     * @Return com.buba.utils.R
     * @throws
     * @Date 2022/6/15 16:41
     **/
    @RequestMapping(value = "/paytoRecord",method = RequestMethod.POST)
    public R paytoRecord(@RequestBody Record record){
        //先查询到今天的日期，格式:"yyyyMMdd"
//        String todayDate = new SimpleDateFormat("yyyyMMdd")
//                .format(new Date());
//        String LSH = "LSH";
//        Object lsh = redisUtil.get("LSH");
//        if (lsh != null){
//            log.info("LSH:::"+lsh);
//            lsh = redisUtil.incr("LSH" , 1);
//        }else{
//            redisUtil.set("LSH" , todayDate+"0001");
//            lsh = redisUtil.get("LSH");
//        }
        record.setDrugRecordSerialNumber(RandomUtil.getSixBitRandom());
        log.info("LSH:::"+record.getDrugRecordSerialNumber());
        boolean save = rService.save(record);
        log.info("记录表自动生成id：：："+record.getId());
        if(save) {
            return R.ok().data("record_id",record.getId());
        }
        else {
            return R.error();
        }
    }

    //住院充值成功详情
    @GetMapping("/recordByrId/{id}")
    public R recordByrId(@PathVariable Integer id){
        RecordVo record = recordService.recordByrId(id);
        return R.ok().data("record" , record);
    }

    @ApiOperation(value = "根据住院人的id查询该住院人购买的药品记录")
    @GetMapping("/selectByInpatientID/{id}")
    public R selectByInpatientID( @ApiParam(name = "id", value = "住院人表的ID", required = true)
                             @PathVariable Integer id) {
        return rService.selectByInpatientID(id);
    }

    @ApiOperation(value = "根据记录表的流水号查询该住院人购买的药品记录")
    @GetMapping("/selectByInpatientrlsh/{drugRecordSerialNumber}")
    public R selectByInpatientrlsh( @ApiParam(name = "drugRecordSerialNumber", value = "记录表的流水号", required = true)
                                    @PathVariable Integer drugRecordSerialNumber) {
        return rService.selectByInpatientrlsh(drugRecordSerialNumber);
    }


    @ApiOperation(value = "根据住院人的id查询该住院人的住院记录和花费")
    @GetMapping("/selectjlByInpatientID/{id}")
    public R selectjlByInpatientID( @ApiParam(name = "id", value = "住院人表的ID", required = false)
                                    @PathVariable Integer id) {
        return rService.selectjlByInpatientID(id);
    }

    @ApiOperation(value = "根据住院记录中的流水号查询该住院人的住院记录和花费")
    @GetMapping("/selectjlByInpatientdrugRecordSerialNumber/{drugRecordSerialNumber}")
    public R selectjlByInpatientdrugRecordSerialNumber(
            @ApiParam(name = "drugRecordSerialNumber", value = "流水号", required = false)
            @PathVariable Integer drugRecordSerialNumber) {
        return rService.selectjlByInpatientdrugRecordSerialNumber(drugRecordSerialNumber);
    }



    // 全查+模糊差
    @PostMapping("/findAllCon")
    public R findAllCon(@RequestBody(required = false) ConsumptionVoWL cVO){
        //模糊查询前端传过来的数据
        log.info("模糊查询前端传过来的数据ConsumptionVo"+cVO);
        PageHelper.startPage(cVO.getPage(),cVO.getLimit());
        //调用分页查询账单类列表的方法
        List<RecordVoWL> rlist = recordService.findAllCon(cVO);
        //创建分页对象
        PageInfo<RecordVoWL> pageInfo = new PageInfo<RecordVoWL>(rlist);
        log.info("List<RecordVoWL>:::"+pageInfo);
        return  R.ok().data("total",pageInfo.getTotal()).data("rows",pageInfo.getList());
    }

    //详情
    @GetMapping("/getByRID/{id}")
    public R getByRID(@PathVariable("id") Integer id){
        return recordService.getByRID(id);
    }


}

